// Copyright (C) 2010 Choonghwan Lee (linjus.lee@gmail.com)
//
// This file is part of amy (http://unitcvt.codeplex.com/).
//
// amy is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// amy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with amy. If not, see <http://www.gnu.org/licenses/>.


function Settings() {
	this.cookieName = "UnitConverterCookie62";
	this.cookieVersion = "0.0.0.12";
	this.precision = 3;
}

var settingInformation = new Settings();

Settings.prototype.reset = function() {
	Mojo.Controller.errorDialog("reset");

	var cookie = new Mojo.Model.Cookie(this.cookieName);
	cookie.remove();
}

Settings.prototype.save = function() {
	var info = unitInformation;
	var current = info.getCurrentCategory();

	this.model = {
		version: this.cookieVersion,
		currentCategory: current.id,
		categories: new Array(),
	};

	for (var i = 0; i < info.categories.length; ++i) {
		var sourceCategory = info.categories[i];
		var savedCategory = new Object();
		savedCategory.id = sourceCategory.id;
		savedCategory.units = new Array();
		for (var j = 0; j < sourceCategory.units.length; ++j) {
			var unit = sourceCategory.units[j];
			var encoded = (unit.visible ? "V" : "H") + unit.id;
			savedCategory.units.push(encoded);
		}
		this.model.categories.push(savedCategory);
	}

	var cookie = new Mojo.Model.Cookie(this.cookieName);
	cookie.put(this.model);
}

Settings.prototype.read = function() {
	var info = unitInformation;

	var cookie = new Mojo.Model.Cookie(this.cookieName);
	var retrieved = cookie.get();
		
	var valid = true;
	if (typeof(retrieved) == undefined || retrieved == null) 
		valid = false;
	else if (retrieved.version != this.cookieVersion) {
		valid = false;
		cookie.remove();
	}

	if (!valid) {
		info.setCurrentCategory(info.categories[0].id);
		return;
	}

	info.setCurrentCategory(retrieved.currentCategory);
	for (var i = 0; i < retrieved.categories.length; ++i) {
		var retrievedCategory = retrieved.categories[i];
		for (var j = 0; j < info.categories.length; ++j) {
			var targetCategory = info.categories[j];
			if (retrievedCategory.id == targetCategory.id) {
				var reordered = new Array();
				for (var k = 0; k < retrievedCategory.units.length; ++k) {
					var retrievedUnit = retrievedCategory.units[k];
					var retrievedVisible = retrievedUnit.substring(0, 1) == "V";
					var retrievedUnitId = retrievedUnit.substring(1);
					for (var l = 0; l < targetCategory.units.length; ++l) {
						var targetUnit = targetCategory.units[l];
						if (retrievedUnitId == targetUnit.id) {
							targetUnit.visible = retrievedVisible;
							reordered.push(targetUnit);
							break;
						}
					}
				}
				info.categories[j].units = reordered;
				break;					
			}
		}
	}
}

